package com.eastfair.auth.controller;

import com.eastfair.auth.dto.RoleDTO;
import com.eastfair.auth.dto.RolePageQuery;
import com.eastfair.auth.dto.RoleSaveDTO;
import com.eastfair.auth.dto.RoleUpdateDTO;
import com.eastfair.auth.service.RoleService;
import com.eastfair.auth.service.UserAccountService;
import com.eastfair.auth.entity.Role;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.auth.vo.RoleVo;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.eastfair.core.base.R.SUCCESS_CODE;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author ligang
 * @date 2021-07-23
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/role")
@Api(value = "Role", tags = "")
public class RoleController extends SuperController<RoleService, Long, Role, RolePageQuery, RoleSaveDTO, RoleUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    UserAccountService userAccountService;

    @Override
    public void handlerResult(IPage<Role> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Role> roleList = list.stream().map((map) -> {
            Role role = Role.builder().build();
            //TODO 请在这里完成转换
            return role;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(roleList));
    }

    /**
     * 创建角色
     * @param roleDTO 角色新增实体
     * @return
     */
    @ApiOperation(value = "创建角色", notes = "创建角色")
    @PostMapping("/createRole")
    public R createRole(@RequestBody @Validated({RoleDTO.createRole.class}) RoleDTO roleDTO) {
        if(roleDTO.getProjectId()==null){
            roleDTO.setProjectId(ContextUtil.getProjectId());
        }
        return baseService.createRole(roleDTO);
    }



    /**
     * 修改角色
     * @param roleDTO 角色新增实体
     * @return
     */
    @ApiOperation(value = "修改角色", notes = "修改角色")
    @PutMapping("/udpateRole")
    public R udpateRole(@RequestBody @Validated({RoleDTO.udpateRole.class}) RoleDTO roleDTO) {
        if(roleDTO.getProjectId()==null){
            roleDTO.setProjectId(ContextUtil.getProjectId());
        }
        return baseService.updateRole(roleDTO);
    }



    /**
     * 删除
     * @param roleIds 角色id集合
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/del")
    public R del(@RequestBody List<Long> roleIds) {
        return baseService.delRole(roleIds);
    }


    /**
     * 剔除角色的所有关系
     * @param roleIds 角色id集合
     * @return
     */
    @ApiOperation(value = "剔除角色的所有关系", notes = "剔除角色的所有关系")
    @PostMapping("/removeRoleRelation")
    public R removeRoleRelation(@RequestBody List<Long> roleIds) {
        return baseService.removeRoleRelation(roleIds);
    }


    /**
     * 剔除角色的菜单关系
     * @param roleIds 角色id集合
     * @return
     */
    @ApiOperation(value = "剔除角色的菜单关系", notes = "剔除角色的菜单关系")
    @PostMapping("/removeRoleRelationOfMenu")
    public R removeRoleRelationOfMenu(@RequestBody List<Long> roleIds) {
        return baseService.removeRoleRelationOfMenu(roleIds);
    }


    /**
     * 重新赋权角色菜单关系
     * @param roleDTO 角色实体
     * @return
     */
    @ApiOperation(value = "重新赋权角色菜单关系", notes = "重新赋权角色菜单关系")
    @PostMapping("/reloadRoleRelationOfMenu")
    public R reloadRoleRelationOfMenu(@RequestBody RoleDTO roleDTO) {
        //校验菜单是否超出该操作员所拥有的菜单
        if(ContextUtil.getUserId()!=null){
            R r = userAccountService.verifyMenu(ContextUtil.getUserId(),roleDTO.getMenuIds());
            if(SUCCESS_CODE != r.getCode()){
                return r;
            }
        }
        return baseService.reloadRoleRelationOfMenu(roleDTO);
    }

    /**
     * 重新赋权角色事件关系
     * @param roleDTO 角色实体
     * @return
     */
    @ApiOperation(value = "重新赋权角色事件关系", notes = "重新赋权角色事件关系")
    @PostMapping("/reloadRoleRelationOfFunction")
    public R reloadRoleRelationOfFunction(@RequestBody RoleDTO roleDTO) {
        //校验事件是否超出该操作员所拥有的事件
        if(ContextUtil.getUserId()!=null){
            R r = userAccountService.verifyFunction(ContextUtil.getUserId(),roleDTO.getFunctionIds());
            if(SUCCESS_CODE != r.getCode()){
                return r;
            }
        }
        return baseService.reloadRoleRelationOfFunction(roleDTO);
    }

    /**
     * 剔除角色的菜单关系
     * @param roleIds 角色id集合
     * @return
     */
    @ApiOperation(value = "剔除角色的事件关系", notes = "剔除角色的事件关系")
    @PostMapping("/removeRoleRelationOfFunction")
    public R removeRoleRelationOfFunction(@RequestBody List<Long> roleIds) {
        return baseService.removeRoleRelationOfFunction(roleIds);
    }



    /**
     * 查询角色旗下菜单，
     * @param id 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "查询角色旗下菜单", notes = "查询角色旗下菜单")
    @GetMapping("/findAllMenuById")
    public R<RoleVo> findAllMenuById(Long id) {
        List<RoleVo> roleVoList = baseService.findAllMenuById(Arrays.asList(id));
        if(roleVoList!=null && !roleVoList.isEmpty()){
            return R.success(roleVoList.get(0));
        }
        return R.success(null);
    }


    /**
     * 查询角色旗下菜单id集合
     * @param id 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "查询角色旗下菜单", notes = "查询角色旗下菜单")
    @GetMapping("/findMenuIdById")
    public R<List<Long>> findMenuIdById(Long id) {
        return R.success(baseService.findMenuIdById(id));
    }



    /**
     * 查询角色和旗下菜单信息
     * @param roleId 角色id
     * @return
     */
    @ApiOperation(value = "查询角色和旗下菜单信息", notes = "查询角色和旗下菜单信息")
    @GetMapping("/getById/{roleId}")
    public R<RoleVo> getById(@PathVariable("roleId") Long roleId) {
        return R.success(baseService.getAllById(roleId));
    }

    /**
     * 查询所有角色
     * @return 查询结果
     */
    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    @GetMapping("/queryAllRole")
    public R<List<RoleVo>> queryAllRole() {
        return R.success(baseService.queryAllRole());
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
//    public void handlerQueryParams(PageParams<RolePageQuery> params) {
//        RolePageQuery rolePageQuery = params.getModel();
//        rolePageQuery.setProjectId(ContextUtil.getProjectId());
//    }


    /**
     * 批量查询角色信息
     * @param ids 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "批量查询角色信息", notes = "批量查询角色信息")
    @PostMapping("/findAllByIds")
    public R<List<RoleVo>> findAllByIds(@RequestBody List<Long> ids) {
        return R.success(baseService.findAllMenuById(ids));
    }




    /**
     * 复制角色
     * @param roleVoList 角色集合
     * @return
     */
    @ApiOperation(value = "复制角色", notes = "复制角色")
    @PostMapping("/copyRole")
    public R<List<RoleVo>> copyRole(@RequestBody List<RoleVo> roleVoList) {
        return R.success(baseService.copyRole(roleVoList));
    }
}
